package com.xyebank.ledger.service.sjdzadd.adaper;

import java.util.Date;

import org.modelmapper.AbstractConverter;
import org.modelmapper.Converter;
import org.modelmapper.ModelMapper;
import org.modelmapper.PropertyMap;
import org.springframework.stereotype.Component;

import com.xyebank.ledger.db.entitys.LPaymentInf;
import com.xyebank.ledger.service.common.conveter.PayBeanConverter;
import com.xyebank.ledger.service.constant.CommonConstant;
import com.xyebank.ledger.service.constant.ProductConstant;
import com.xyebank.ledger.util.DateUtil;
import com.xyebank.ledger.util.IDUtil;
import com.xyebank.ledger.vo.deal.ZADDSjdCashCreditVo;

@Component(value = "zaddSjdCreditBeanConverter")
public class ZADDSjdCreditBeanConverter implements PayBeanConverter<ZADDSjdCashCreditVo> {

	ModelMapper modelMapper = new ModelMapper();

	Converter<String, Date> dateConverter = new AbstractConverter<String, Date>() {
		protected Date convert(String source) {
			return source == null ? null : DateUtil.getDate(source);
		}
	};

	class NXApplyPropertyMap extends PropertyMap<ZADDSjdCashCreditVo, LPaymentInf> {
		// 注意 不要有重复的映射，不然会报错
		protected void configure() {
			// 享宇订单号
			map(source.getSsjLoanNo(), destination.getXyOrderNo());
			// 申请人电话号码
			map(source.getMblNo(), destination.getMblNo());
			// 业务平台
			map(source.getChannel(), destination.getDataSource());
			// 提现审批状态
			// map(source.getLoanStatus(), destination.getApprovalStatus());
			// 提现审批时间
			using(dateConverter).map(source.getApproveTime(), destination.getApprovalTime());
			// 放款状态
			// map(source.getLoanStatus(), destination.getLoanStatus());
			// 申请提现金额
			map(source.getApplyAmount(), destination.getApplyAmount());
			// 申请提现时间
			using(dateConverter).map(source.getApplyTime(), destination.getApplyTime());
			// 放款金额
			map(source.getLoanAmount(), destination.getLoanAmount());
			// 放款时间
			// map(source.getLendIngtime(), destination.getLoanTime());
			// 放款期数
			map(source.getTerm(), destination.getLoanPeriod());
			// 提现订单号
			map(source.getuid(), destination.getOrgOrderNo());

		}
	}

	public ZADDSjdCreditBeanConverter() {
		modelMapper.addMappings(new NXApplyPropertyMap());
	}

	@Override
	public LPaymentInf covert(ZADDSjdCashCreditVo x) {
		LPaymentInf entity = modelMapper.map(x, LPaymentInf.class);
		entity.setOrgNo(ProductConstant.ZADD_LOAN_ORG);
		entity.setProductNo(ProductConstant.ZADD_PROD_ID);
		entity.setId(IDUtil.generateId(x.getpid(), "zadd_sjd_creat"));
		entity.setLoanTime(DateUtil.getStrDate(x.getLendIngtime()));
		String status = x.getLoanStatus();
		if ("9".equals(status)) {
			entity.setApprovalStatus(CommonConstant.LOAN_SUCCESS);
			entity.setLoanStatus(CommonConstant.LOAN_SUCCESS);
		} else {
			entity.setApprovalStatus(CommonConstant.LOAN_fail);
			entity.setLoanStatus(CommonConstant.LOAN_fail);
		}
		return entity;
	}
}
